/*
 * ReimsCopieAccessBean.java
 *
 * Created on 9 janvier 2008, 14:56
 */
package com.reimscopie.beans.session ;

import javax.persistence.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.entities.* ;


/**
 * This abstract class provides some utilities to access shared datas.
 */
public abstract class ReimsCopieAccessBean extends ReimsCopieSessionBean {
  
  /**
   * Create a new <code>ReimsCopieAccessBean</code> instance.
   */
  protected  ReimsCopieAccessBean() {
    
  }
  
  
  /**
   * Return this persistence manager used by this bean.
   *
   * @return The persistence manager to use.
   */
  protected abstract EntityManager getPersistenceManager() ;
  
  
////////////////////////////////////////////////////////////////////////////////
//                               BON-PLANS                                    //
////////////////////////////////////////////////////////////////////////////////
  
  
  /**
   * Return the specified bon plan.
   *
   * @param id The bon plan identifier.
   */
  public BonPlan getBonPlan(long id) {
    
    EntityManager manager = getPersistenceManager() ;
    
    BonPlanBean bean = getEntity(manager,BonPlanBean.class,id) ;
    
    // not found
    if (bean == null)
      return null ;
    
    return bean.toValue() ;
  }
  
  
  /**
   * Return all bon plans.
   *
   * @return All bon plans.
   */
  public BonPlan[] getBonPlans() {
    
    EntityManager manager = getPersistenceManager() ;
    
    Object[] objects = executeMultipleQuery(manager,"all-bon-plans") ;
    
    BonPlan[] bonPlans = new BonPlan[objects.length] ;
    for (int i=0; i<objects.length;i++) {
      BonPlanBean bean = (BonPlanBean)objects[i] ;
      bonPlans[i] = bean.toValue() ;
    }
    
    return bonPlans ;
  }
  
  
  /**
   * Return the best bon-plans.
   *
   * @return The best bons-plans.
   */
  public BonPlan getBestBonPlan() {
    
    BonPlan[] all  = getBonPlans() ;
    
    if (all == null  ||  all.length == 0)
      return null ;
    
    int index = 0 ;
    int best  = all[0].getFactor() ;
    for (int i=1;i<all.length;i++) {
      if (all[i].getFactor() > best) {
        index = i ;
        best  = all[i].getFactor() ;
      }
    }
    
    return all[index] ;
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                                PRODUCT                                     //
////////////////////////////////////////////////////////////////////////////////
  
  /**
   * Return the simple product with the specified id.
   *
   * @param id The product id.
   *
   * @return The specified simple product, or <code>null</code> if the id was
   * not found.
   */
  public SimpleProduct getSimpleProduct(long id) {
    
    EntityManager manager = getPersistenceManager() ;
    
    SimpleProductBean bean = getEntity(manager,SimpleProductBean.class,id) ;
    
    // not found
    if (bean == null)
      return null ;
    
    return bean.toValue() ;
    
  }
 
  
  /**
   * Get the list of all available SimpleProduct.
   *
   * @return All available simple products.
   */
  public SimpleProduct[] getSimpleProducts() {
    
    EntityManager manager = getPersistenceManager() ;
    
    Object[] objects = executeMultipleQuery(manager,"all-simple-products") ;
    
    SimpleProduct[] products = new SimpleProduct[objects.length] ;
    for (int i=0; i<objects.length;i++) {
      SimpleProductBean bean = (SimpleProductBean)objects[i] ;
      products[i] = bean.toProduct() ;
    }
    
    return products ;
  }
  
  
  /**
   * Get the list of all available SimpleProduct with the valid status.
   *
   * @return All available and valid simple products.
   */
  public SimpleProduct[] getValidSimpleProducts() {
    
    EntityManager manager = getPersistenceManager() ;
    
    Object[] objects = executeMultipleQuery(manager,"all-valid-simple-products") ;
    
    SimpleProduct[] products = new SimpleProduct[objects.length] ;
    for (int i=0; i<objects.length;i++) {
      SimpleProductBean bean = (SimpleProductBean)objects[i] ;
      products[i] = bean.toProduct() ;
    }
    
    return products ;
  }
  
  /**
   * Return the special product with the specified id.
   *
   * @param id The product id.
   *
   * @return The specified special product, or <code>null</code> if the id was
   * not found.
   */
  public SpecialProduct getSpecialProduct(long id) {
    
    EntityManager manager = getPersistenceManager() ;
    
    SpecialProductBean bean = getEntity(manager,SpecialProductBean.class,id) ;
    
    // not found
    if (bean == null)
      return null ;
    
    return bean.toValue() ;
    
  }
 
  
  /**
   * Get the list of all available SpecialProduct.
   *
   * @return All available special products.
   */
  public SpecialProduct[] getSpecialProducts() {
    
    EntityManager manager = getPersistenceManager() ;
    
    Object[] objects = executeMultipleQuery(manager,"all-special-products") ;
    
    SpecialProduct[] products = new SpecialProduct[objects.length] ;
    for (int i=0; i<objects.length;i++) {
      SpecialProductBean bean = (SpecialProductBean)objects[i] ;
      products[i] = bean.toProduct() ;
    }
    
    return products ;
  }
  
  
  /**
   * Get the list of all available SpecialProduct with the valid status.
   *
   * @return All available and valid special products.
   */
  public SpecialProduct[] getValidSpecialProducts() {
    
    EntityManager manager = getPersistenceManager() ;
    
    Object[] objects = executeMultipleQuery(manager,"all-valid-special-products") ;
    
    SpecialProduct[] products = new SpecialProduct[objects.length] ;
    for (int i=0; i<objects.length;i++) {
      SpecialProductBean bean = (SpecialProductBean)objects[i] ;
      products[i] = bean.toProduct() ;
    }
    
    return products ;
  }  
  
  
  /**
   * Return the product with the specified id. The product can be of any kind.
   *
   * @param id The product id.
   *
   * @return The specified product, or <code>null</code> if the id was not
   * found.
   */
  public Product getProduct(long id) {
    
    EntityManager manager = getPersistenceManager() ;
    
    ProductBean bean = getEntity(manager,ProductBean.class,id) ;
    
    // not found
    if (bean == null)
      return null ;
    
    return bean.toProduct() ;
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                                COMMENT                                     //
////////////////////////////////////////////////////////////////////////////////
  
  /**
   * Return the comment with the specified id.
   *
   * @param id The comment id.
   *
   * @return The specified comment, ou <code>null</code> if the comment was
   * not found.
   */
  public Comment getComment(long id) {
    
    EntityManager manager = getPersistenceManager() ;
    
    return getValue(manager,CommentBean.class,id) ;
    
  }
  
  
  /**
   * Create a new comment. The comment is stored by the persistence manager.
   *
   * @param comment The comment to create.
   * @param productId The product where to add the comment.
   */
  public void createComment(Comment comment,long productId) {
    EntityManager entityManager = getPersistenceManager() ;
    // instance
    CommentBean commentBean = new CommentBean(comment) ;
    ProductBean productBean = getEntity(entityManager,ProductBean.class,productId);
    
    // link
    AccountBean accountbean = getEntity(entityManager,AccountBean.class,
        comment.getAccount().getId());
    commentBean.setAccount(accountbean);
    
    productBean.addComment(commentBean) ;
    
    // persistence
    entityManager.persist(commentBean) ;
  }
}
